package org.dromara.resource.api.feign.fallback;

import cn.hutool.http.HttpStatus;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.resource.api.feign.ResourceMessageClient;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author: zhou shuai
 * @date: 2024/10/5 15:36
 * @version: v1
 */
@Slf4j
@Component
public class ResourceMessageFallBack implements FallbackFactory<ResourceMessageClient> {

    @Override
    public ResourceMessageClient create(Throwable throwable) {
        log.error("ResourceMessageFallBack, cause:{}", throwable.getMessage());
        return new ResourceMessageClient() {

            @Override
            public R<Void> publishMessage(List<Long> sessionKeyList, String message) {
                return R.fail(HttpStatus.HTTP_BAD_METHOD, throwable.getLocalizedMessage());
            }

            @Override
            public R<Void> publishAll(String message) {
                return R.fail(HttpStatus.HTTP_BAD_METHOD, throwable.getLocalizedMessage());
            }
        };
    }

}
